<template>
  <el-form :inline="true" :model="queryInfo" class="demo-form-inline">
    <el-form-item label="标题名">
      <el-input v-model="queryInfo.freeName" placeholder="请输入" clearable />
    </el-form-item>
    <el-form-item label="编号">
      <el-input v-model="queryInfo.freeId" placeholder="请输入" clearable />
    </el-form-item>
    <el-form-item>
        <el-button type="primary" @click="getFreeList">搜索</el-button>
    </el-form-item>
  </el-form>
  <div>
    <el-button @click="toAdd" type="primary" icon="Plus" plain>新增</el-button>
  </div>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column type="selection" width="55" />
    <el-table-column fixed prop="freeId" label="编号" width="80" />
    <el-table-column prop="freeName" label="标题名" />
    <el-table-column prop="freeSimplyintroduce" label="简介" />
    <el-table-column prop="freeText" label="详情" />
    <el-table-column prop="freeCreatetime" label="创建时间" width="100"/>
    <el-table-column prop="freeImg" label="图片">
      <template #default="scope">
        <img  v-if="scope.row.freeImg" :src="url + scope.row.freeImg" style="width: 50px;height: 50px;">
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.row)">
          修改 
        </el-button>
        <el-button size="small" type="danger" @click="handleDelete(scope.row.freeId)">
          删除
        </el-button>
      </template>
      </el-table-column>
  </el-table>
  <!-- 分页 -->
  <el-pagination
      v-model:current-page="queryInfo.pageNum"
      v-model:page-size="queryInfo.pageSize"
      :page-sizes="[1, 5, 10, 20]"
      :size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  <!-- 添加修改 对话框 -->
  <el-dialog v-model="saveOrUpdateVisible" :title="saveOrUpdateTitle" width="500">
    <el-form :model="Free">
      <el-form-item label="标题" :label-width="formLabelWidth">
        <el-input v-model="Free.freeName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="简介" :label-width="formLabelWidth">
        <el-input v-model="Free.freeSimplyintroduce" autocomplete="off" />
      </el-form-item>
      <el-form-item label="详情" :label-width="formLabelWidth">
        <el-input v-model="Free.freeText" autocomplete="off" />
      </el-form-item>
      <el-form-item label="创建时间" :label-width="formLabelWidth">
        <el-input v-model="Free.freeCreatetime" autocomplete="off" />
      </el-form-item>
      <el-form-item label="图片" :label-width="formLabelWidth">
        <el-input v-model="Free.freeImg" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="saveOrUpdate">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'
import { reactive, ref, onMounted } from 'vue'
const url = 'http://localhost:8080/file/download?fileName='
const saveOrUpdateVisible = ref(false)
const saveOrUpdateTitle = ref('添加数据内容表')
const tableData = ref([])
const total = ref()
const formLabelWidth = ref('100px');
const freeImg = ref('')
const Free = ref({
  freeId: '',
  freeName: '',
  freeImg: '',
  freeSimplyintroduce: '',
  freeText: '',
  freeCreatetime: ''
});
const queryInfo = ref({
    pageNum: 1,
    pageSize: 5,
    freeName: '',
    freeId: '' 
})
  // 图片上传保存
const handleAvatarSuccess = (response, uploadFile) => {
  freeImg.value = URL.createObjectURL(uploadFile.raw);
  // 假设后端返回的响应中包含上传成功的文件路径
  Free.value.freeImg = response.data;

} 
// 查询内容列表
const getFreeList = () => {
    axios.get('/free/findByPage', { params: queryInfo.value })
    .then(resp => {
        console.log(resp.data)
        let result = resp.data;
        total.value = result.data.total
        if (result.code == 1) {
          tableData.value = result.data.list
        } else {
          console.log('获取列表失败')
        }
      })
}
onMounted(()=>{
    getFreeList();
})
// 分页
// 当用户改变每页显示的条目数时触发
const handleSizeChange = (pageSize) => {
  queryInfo.value.pageSize = pageSize;
  getFreeList();
}
// 当用户改变当前页码时触发
const handleCurrentChange = (pageNum) => {
  queryInfo.value.pageNum = pageNum;
  getFreeList();
}
// 保存或更新
const saveOrUpdate = () => {
  if (saveOrUpdateTitle.value == '添加数据内容表') {
    // 调用后端接口添加
    axios.post('/free/insert',Free.value)
      .then(resp => {
        let result = resp.data;  // resp是promise对象，.data返回我们自己的标准R
        if (result.code == 1) {
          // 重新获取列表
          getFreeList();
          ElMessage.success('添加文章成功')
        } else {
          ElMessage.warning('添加文章失败')
        }
      })
  } else {
    // 调用后端接口修改
    axios.put('/free/update', Free.value)
      .then(resp => {
        let result = resp.data;  // resp是promise对象，.data返回我们自己的标准R
        if (result.code == 1) {
          // 重新获取列表
          getFreeList();
          ElMessage.success('修改文章成功')
        } else {
          ElMessage.warning('修改文章失败')
        }
      });
  }
  
  // 关闭弹框
  saveOrUpdateVisible.value = false;
  Free.value = { 
    freeId: '', 
    freeName: '',
    freeImg: '',
    freeSimplyintroduce: '',
    freeText: '',
    freeCreatetime: ''
  };
  freeImg.value = '';
}
  // 修改
  const handleEdit = (row) => {
    saveOrUpdateTitle.value = '修改数据内容表'
    saveOrUpdateVisible.value = true
    Free.value = row
    freeImg.value = url + row.freeImg
  }
  // 新增
const toAdd = () => {
    saveOrUpdateTitle.value = '添加数据内容表'
    saveOrUpdateVisible.value = true

}
  // 取消修改或保存
const close = () => {
  saveOrUpdateVisible.value = false;
  Free.value = { 
    freeId: '', 
    freeName: '', 
    freeImg: '', 
    freeSimplyintroduce: '', 
    freeText: '', 
    freeCreatetime: ''
  };
}
  

 
  // 删除
const handleDelete = (freeId) => {
    ElMessageBox.confirm(
        '真的要删除吗？',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        })
        .then(() => {
            axios.delete('/free/delete', {
                params: { id: freeId }
            })
                .then(resp => {
                    let result = resp.data;
                    if (result.code == 1) {
                        getFreeList();
                        ElMessage({
                            message: '删除成功！',
                            type: 'success',
                        })
                    } else {
                        ElMessage({
                            message: '删除失败！',
                        })
                    }
                })
        })
}

</script>

  <style scoped>
  .demo-form-inline .el-input {
    --el-input-width: 220px;
  }

  .demo-form-inline .el-select {
    --el-select-width: 220px;
  }
  .avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
  .avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
  }

  .avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
  }

  .el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
  }
</style>